<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/15/015
 * Time: 11:35
 */

namespace app\api\controller;


use app\admin\model\Seller;
use app\common\controller\Api;
use think\Db;

class Usercartitem extends Api
{
    protected $noNeedLogin = ['*'];

    public function index()
    {
        $user_id = $this->request->request('user_id');
        $seller_id = $this->request->request('seller_id');
        if (!\app\common\model\User::get($user_id)) {
            $this->error('没有这个用户');
        }
        if(!Seller::get($seller_id)){
            $this->error('没有这个商家');
        }
        $model = new \app\api\model\Usercartitem();
        $cartlist = $model->where('user_id' , $user_id)
            ->where('seller_id' , $seller_id)
            ->where('status', 1)
            ->select();
        $this->success('',$cartlist);
    }

    public function add()
    {
        $user_id = $this->request->request('user_id');
        $num = $this->request->request('num');
        $seller_id = $this->request->request('seller_id');
        if (!\app\common\model\User::get($user_id)) {
            $this->error('没有这个用户');
        }
        if(!Seller::get($seller_id)){
            $this->error('没有这个商家');
        }
        $sellerproduct_id = $this->request->request('sellerproduct_id');
        $product = \app\api\model\Sellerproduct::get($sellerproduct_id);
        if (!$product) {
            $this->error('没有这个商家产品');
        }

        $model = new \app\api\model\Usercartitem();
        $result = $model->allowField(true)->validate(true)->save([
            'seller_id' => $seller_id,
            'user_id' => $user_id,
            'sellerproduct_id' => $sellerproduct_id,
            'num' => $num,
            'price' => $product['price'],
            'productname' => $product['name'],
            'image' => $product['image'],
            'totalprice' => $product['price'] * $num,
            'status' => 1,
        ]);
        if ($result) {
            $this->success('成功');
        }
        $this->error($model->getError());
    }

    public function setNum()
    {
        $id = $this->request->request('id');
        $num = $this->request->request('num');

        if (!$id || !$num) {
            $this->error(__('Invalid parameters'));
        }
        //当前登录用户只能修改自己的购车数量
        $cartitem = \app\api\model\Usercartitem::get($id);
        if (!$cartitem) {
            $this->error('没有这个购物车项');
        }

        $usercart = new \app\api\model\Usercartitem();
        $result = $usercart->save(['num' => $num,'totalprice' => $num * $cartitem['price']],['id' => $id]);
        if ($result) {
            $this->success('成功');
        }
        $this->error('失败');
    }

    public function delete()
    {
        $id = $this->request->request('id');
        if(!$id){
            $this->error(__('Invalid parameters'));
        }
        $model = \app\api\model\Usercartitem::get($id);
        if (!$model) {
            $this->error('找不到这条数据');
        }
        $result =$model->delete();
        if ($result) {
            $this->success('成功');
        }
        $this->error($model->getError());
    }
}